import time
from DrissionPage import ChromiumPage, ChromiumOptions
bro = ChromiumPage()
bro.get('https://pic.netbian.com/4kmeinv/')  # 爬取网站地址
def get_url(pic_lists):
    p = 0
    for pic_pic in pic_lists:

        if p == len(pic_lists) - 1:    # 在判断有“下一页”选项，最后一个单独处理。
            print(f"Clicking on element: {pic_pic.html}")
            pic_pic.click()
            time.sleep(3)
        else:
            download(pic_pic)
        p += 1
def download(pic_pic):
    pic_pic.click()
    tab1 = bro.latest_tab
    url = tab1.ele('xpath://*[@id="img"]/img').attr('src')  # 获取子页面url
    name = tab1.ele('xpath://*[@id="img"]/img').attr('title')  # 获取图片标题
    if '3840x2400' in name:
        tab2 = bro.new_tab(url)
        img = tab2.ele('xpath:/html/body/img')  # 定位元素
        img.save(path='tuku', name=f'{name}.jpg')  # 保存图片
        print(f"{name}::: 下载成功！")
        tab2.close()
    tab1.close()
while True:  # 多网页爬取
    print(bro.url)
    pic_lists = bro.eles('xpath://*[@id="main"]/div[4]/ul/li')
    if '下一页' in pic_lists[-1].html and '更多精彩' in pic_lists[-1].html:  # 判断是否有下一页选项存在
        get_url(pic_lists)
    else:                       # 不存在，则只运行下载程序并退出循环。
        for pic_pic in pic_lists:
            download(pic_pic)
        break



